package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.JsProductService;
import com.pro.utils.DateUtils;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author YuQing
 * @date 2022/1/7 9:11
 */
@Controller
@RequestMapping("/product")
public class JsProductController {
    @Autowired
    private JsProductService jsProductService;
    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    //获取商品分类
    @RequestMapping(value = "/getProductCategroys")
    @ResponseBody
    public JsonData getSyProductCategroys(){
        List<JsProductCategroy> syProductCategroys = jsProductService.getProductCategroys();
        return syProductCategroys != null ? JsonData.buildSuccess(syProductCategroys) : JsonData.buildError("服务器出错了");
    }

    //判断权限
    @RequestMapping("/judgePower")
    @ResponseBody
    public JsonData judgePower(Integer id){
        List<Integer> list = jsProductService.judgePower(id);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取ztree
    @RequestMapping("/getProductTree")
    @ResponseBody
    public JsonData getProductTree() {
        List<JsProductCategroy> list = jsProductService.getProductTree();
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }
    //添加树节点
    @RequestMapping("/addProductTree")
    @ResponseBody
    public JsonData addProductTree(JsProductCategroy productCategroy){
        boolean addResult = jsProductService.addZtree(productCategroy);
        return addResult?JsonData.buildSuccess(addResult):JsonData.buildError("服务器出错了");
    }
    //获取删除节点下商品数量
    @RequestMapping("/getProductTreeCount")
    @ResponseBody
    public JsonData getProductTreeCount(Integer categroyId){
        Integer productTreeCount = jsProductService.getProductTreeCount(categroyId);
        return productTreeCount != null?JsonData.buildSuccess(productTreeCount):JsonData.buildError("服务器出错了");
    }
    //删除树节点
    @RequestMapping("/deleteProductTree")
    @ResponseBody
    public JsonData deleteProductTree(Integer categroyId){
        boolean deleteResult = jsProductService.deleteZtree(categroyId);
        return deleteResult?JsonData.buildSuccess(deleteResult):JsonData.buildError("服务器出错了");
    }
    //修改树节点
    @RequestMapping("/updateProductTree")
    @ResponseBody
    public JsonData updateProductTree(String title,Integer categroyId){
        boolean updateResult = jsProductService.updateZtree(title, categroyId);
        return updateResult?JsonData.buildSuccess(updateResult):JsonData.buildError("服务器出错了");
    }
    //点击树节点获取信息
    @RequestMapping("/getCheckedProduct")
    @ResponseBody
    public JsonData getCheckedProduct(JsProduct product){
        List<JsProduct> list = jsProductService.getCheckedProduct(product);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }
    //获取表数据
    @RequestMapping("/getProductSearch")
    @ResponseBody
    public JsonData getProductSearch(JsProduct product) {
        List<JsProduct> productSearch = jsProductService.getProductSearch(product);
        return productSearch!=null?JsonData.buildSuccess(productSearch): JsonData.buildError("服务器出错了");
    }
    //高级检索
    @RequestMapping("/advancedSearch")
    @ResponseBody
    /**
     * @return: com.pro.pojo.JsonData
     * @author: YuQing
     * @dateTime: 2022/1/11 15:46
     */
    public JsonData advancedSearch(JsProduct product) {
        List<JsProduct> jsProducts = jsProductService.advancedSearch(product);
        return jsProducts!=null?JsonData.buildSuccess(jsProducts): JsonData.buildError("服务器出错了");
    }
    //查看商品详情
    @RequestMapping("/getProductInformation")
    @ResponseBody
    /**
     * @return: com.pro.pojo.JsonData
     * @author: YuQing
     * @dateTime: 2022/1/11 20:28
     */
    public JsonData getProductInformation(Integer type, Integer id) {
        JsProduct productInformation = jsProductService.getProductInformation(type, id);
        return productInformation!=null?JsonData.buildSuccess(productInformation): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/updateProductState")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/15
     * @description: 修改商品状态
     */
    public JsonData updateProductState(JsProduct product){
        String date = DateUtils.tranferDateTimeToString(new Date());
        product.setUpdateDate(date);
        Boolean updateState = jsProductService.updateProductState(product);
        return updateState!=null?JsonData.buildSuccess(updateState): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getProductExamine")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/15
     * @description: 查询商品审核详情
     */
    public JsonData getProductExamine(JsCourseRecord courseRecord){
        List<JsCourseRecord> productExamine = jsProductService.getProductExamine(courseRecord);
        return productExamine!=null?JsonData.buildSuccess(productExamine): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/addProductLog")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/15
     * @description: 添加商品操作日志
     */
    public JsonData addProductLog(JsLog log){
        String date = DateUtils.tranferDateTimeToString(new Date());
        log.setCreateDate(date);
        Boolean addResult = jsProductService.addProductLog(log);
        return addResult!=null?JsonData.buildSuccess(addResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/updateProduct")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/16
     * @description: 修改商品
     */
    public JsonData updateProduct(JsProduct product){
        String date = DateUtils.tranferDateTimeToString(new Date());
        product.setUpdateDate(date);
        Boolean updateResult = jsProductService.updateProduct(product);
        return updateResult!=null?JsonData.buildSuccess(updateResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/addProductJournal")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/16
     * @description: 添加历史价格详情
     */
    public JsonData addProductJournal(JsCourseJournal courseJournal){
        String date = DateUtils.tranferDateTimeToString(new Date());
        courseJournal.setUpdateDate(date);
        Boolean addResult = jsProductService.addProductJournal(courseJournal);
        return addResult!=null?JsonData.buildSuccess(addResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getProductLog")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/17
     * @description: 查询商品操作日志
     */
    public JsonData getProductLog(JsLog log){
        if(log.getCreateDate() != null && log.getCreateDate() != ""){
            log.setCreateDate(log.getCreateDate().substring(0,10));
        }
        List<JsLog> productLog = jsProductService.getProductLog(log);
        return productLog!=null?JsonData.buildSuccess(productLog): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/deleteProductLog")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/17
     * @description: 删除商品操作日志
     */
    public JsonData deleteProductLog(Integer id){
        Boolean deleteResult = jsProductService.deleteProductLog(id);
        return deleteResult!=null?JsonData.buildSuccess(deleteResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getProductPriceLog")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/18
     * @description: 查看商品历史价格详情
     */
    public JsonData getProductPriceLog(JsCourseJournal courseJournal){
        if(courseJournal.getUpdateDate() != null && courseJournal.getUpdateDate() != ""){
            courseJournal.setUpdateDate(courseJournal.getUpdateDate().substring(0,10));
        }
        List<JsCourseJournal> productPriceLog = jsProductService.getProductPriceLog(courseJournal);
        return productPriceLog!=null?JsonData.buildSuccess(productPriceLog): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/deleteProduct")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/19
     * @description: 删除商品
     */
    public JsonData deleteProduct(Integer id){
        Boolean deleteResult = jsProductService.deleteProduct(id);
        return deleteResult!=null?JsonData.buildSuccess(deleteResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/deleteProductRecord")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/19
     * @description: 删除商品审核
     */
    public JsonData deleteProductRecord(Integer id,Integer type){
        Boolean deleteResult = jsProductService.deleteProductRecord(id,type);
        return deleteResult!=null?JsonData.buildSuccess(deleteResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/deleteProductComment")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/19
     * @description: 删除商品评论
     */
    public JsonData deleteProductComment(Integer id,Integer type){
        Boolean deleteResult = jsProductService.deleteProductComment(id,type);
        return deleteResult!=null?JsonData.buildSuccess(deleteResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getPersonalProduct")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/19
     * @description: 查询个人商品
     */
    public JsonData getPersonalProduct(JsProduct product){
        List<JsProduct> personalProduct = jsProductService.getPersonalProduct(product);
        return personalProduct!=null?JsonData.buildSuccess(personalProduct): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/addCourseRecord")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/22
     * @description: 添加商品审核
     */
    public JsonData addCourseRecord(JsCourseRecord courseRecord){
        String date = DateUtils.tranferDateTimeToString(new Date());
        courseRecord.setApplyDate(date);
        Boolean addResult = jsProductService.addCourseRecord(courseRecord);
        return addResult!=null?JsonData.buildSuccess(addResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping(value = "/uploadImg")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/23
     * @description: 添加商品图片
     */
    public JsonData uploadImg(@RequestParam("image") MultipartFile file){
        if(file.getSize() > 0){
            //数据库文件路径
            String fileUrl = null;
            try {
                fileUrl = fastDFSClientUtil.uploadFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            HashMap<String, Object> map = new HashMap<>();
            //前台传递路径
            String src = fastDFSClientUtil.getResAccessUrl(fileUrl);
            map.put("code", 200);
            map.put("src", src);
            return map.get("src") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传失败");
        }
        return null;
    }
    @RequestMapping("/addProduct")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/23
     * @description: 添加商品
     */
    public JsonData addProduct(JsProduct product){
        String date = DateUtils.tranferDateTimeToString(new Date());
        product.setCreateDate(date);
        Boolean addResult = jsProductService.addProduct(product);
        return addResult!=null?JsonData.buildSuccess(addResult): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getProductCode")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/24
     * @description: 获取商品编号
     */
    public JsonData getProductCode(Integer productCode){
        Integer code = jsProductService.getProductCode(productCode);
        return code!=null?JsonData.buildSuccess(code): JsonData.buildError("服务器出错了");
    }
    @RequestMapping("/getProductId")
    @ResponseBody
    /**
     * @author: YuQing
     * @date: 2022/1/24
     * @description: 获取商品id
     */
    public JsonData getProductId(Integer productCode){
        JsProduct code = jsProductService.getProductId(productCode);
        return code!=null?JsonData.buildSuccess(code): JsonData.buildError("服务器出错了");
    }
}
